package moodish.client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.DefaultRowSorter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.table.DefaultTableModel;

import moodish.comm.component.ClientCommComponent;

public class ClientGUI {

	// Global data
	private static Object[][] data = {};
	private static String[] columnNames = { "Nickname", "Mood", "Type" };

	private ClientCommComponent clientComm = new ClientCommComponent();
	private JPanel actionPanel_s = new JPanel();
	private JPanel actionPanel_n = new JPanel();
	private JPanel actionPanel_c = new JPanel();
	private JFrame guiFrame = new JFrame();
	private JTextField text = new JTextField(25);
	private static DefaultTableModel dtm = new DefaultTableModel(data,
			columnNames);
	private JTable jtable = new JTable(dtm);

	private String username = "";
	// private static ServerCommComponent serverCommComponent = new
	// ServerCommComponent();
	private ClientGuiThread guiThread;

	// dtm.addRow(new Object[] { "Nickname", "Moodish Messge" , "Type" });
	// public static void main(String[] arguments) {
	public ClientGUI() {

		// jtable.setAutoCreateRowSorter(true);
		jtable.getColumnModel().getColumn(0).setPreferredWidth(100);
		jtable.getColumnModel().getColumn(1).setPreferredWidth(300);
		jtable.getColumnModel().getColumn(2).setPreferredWidth(200);

		// GUI construction
		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		guiFrame.setTitle("Moodish Client");
		guiFrame.setSize(800, 600);
		guiFrame.setLocationRelativeTo(null);
		guiFrame.add(actionPanel_n, BorderLayout.NORTH);
		guiFrame.add(actionPanel_c, BorderLayout.CENTER);
		guiFrame.add(actionPanel_s, BorderLayout.SOUTH);

		// options button
		JButton btnConnect = new JButton("Connect Server");
		JButton btnDisconnect = new JButton("Disconnect Server");
		actionPanel_n.add(btnConnect);
		actionPanel_n.add(btnDisconnect);

		btnConnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!text.getText().isEmpty()) {
					try {
						clientComm.connect("localhost", text.getText());
						guiFrame.setTitle("Moodish Client - " + text.getText());
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					JOptionPane.showMessageDialog(null, "Connect to server");
					username = text.getText();
					dtm.addRow(new Object[] { username, "",
							"Connected " + text.getText() });
					text.setText("");
				} else {
					JOptionPane.showMessageDialog(null, "TextField is null!");
				}
			}
		});

		btnDisconnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				clientComm.disconnect();
				JOptionPane.showMessageDialog(null, "Disconnect from server!");
			}
		});

		JButton btnFriend = new JButton("Friend");
		JButton btnUnfriend = new JButton("Unfriend");
		JButton btnSendmsg = new JButton("Send Message");

		actionPanel_s.add(text);
		actionPanel_s.add(btnFriend);
		actionPanel_s.add(btnUnfriend);
		actionPanel_s.add(btnSendmsg);

		btnFriend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (username != "") {
					if (!text.getText().isEmpty()) {
						clientComm.friendship(text.getText());
						JOptionPane.showMessageDialog(null, "Friendship send!");
						dtm.addRow(new Object[] { username, "",
								"FriendShip sent to " + text.getText() });
						text.setText("");
					} else {
						JOptionPane.showMessageDialog(null,
								"TextField is null!");
					}
				} else {
					JOptionPane.showMessageDialog(null, "Connect first!");
				}

			}
		});

		btnUnfriend.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (username != "") {
					if (!text.getText().isEmpty()) {
						clientComm.unfriendship(text.getText());
						JOptionPane.showMessageDialog(null,
								"Unfriendship done!");
						text.setText("");
					} else {
						JOptionPane.showMessageDialog(null,
								"TextField is null!");
					}
				} else {
					JOptionPane.showMessageDialog(null, "Connect first!");
				}
			}
		});

		btnSendmsg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (username != "") {
					if (!text.getText().isEmpty()) {
						clientComm.sendMoodishMessage(text.getText());
						dtm.addRow(new Object[] { username, text.getText() });
						text.setText("");
					} else {
						JOptionPane.showMessageDialog(null,
								"TextField is null!");
					}
				} else {
					JOptionPane.showMessageDialog(null, "Connect first!");
				}
			}
		});

		actionPanel_c.add(jtable);
		guiFrame.setVisible(true);

		guiThread = new ClientGuiThread(clientComm, dtm);
		guiThread.start();
	}
}